<?php

declare(strict_types=1);

namespace App\Chat\Controller;

use _PHPStan_c4c026984\Nette\Schema\Context;
use App\Chat\Annotation\ChatAuth;
use App\Chat\Middleware\AddParamsMiddleware;
use App\Chat\Request\TalkKeywordRequest;
use App\CustHub\Service\ChTalkKeywordService;
use App\CustHub\Service\WordService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: 'ch/chat/keyword'),ChatAuth]
class KeywordController extends ChatBaseController
{
    #[Inject]
    protected ChTalkKeywordService $service;

    #[Inject]
    protected WordService $wordService;


    /**
     * 关键字列表
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('list')]
    public function list(): \Psr\Http\Message\ResponseInterface
    {
        $data = $this->request->all();
        $data['select'] = 'id,title,match_type,keyword,content,msg_types';
        $data['main_uid'] =  \Hyperf\Context\Context::get('main_uid');
        return $this->success($this->wordService->getPageList($data,false));
    }


    /**
     * 新增
     * @param TalkKeywordRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('save')]
    public function save(TalkKeywordRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data = $request->all();
        $groupData['main_uid'] =  \Hyperf\Context\Context::get('main_uid');
        $groupData['uid'] = \Hyperf\Context\Context::get('m_uid');
        $groupData['group_id'] = $data['group_id'];
        $groupData['is_personal'] = $data['type'];
        if($data['type'] == 1){
            $groupData['cs_id'] = $data['uid'];
        }else{
            $groupData['cs_id'] = 0;
        }
        $groupData['title'] = $data['title'];
        $groupData['keyword'] = $data['keyword'];
        $groupData['match_type'] = $data['match_type'];
        $groupData['content'] = $data['content'];
        return $this->wordService->save($groupData) ? $this->success() : $this->error();
    }

    /**
     * 更新
     * @param TalkKeywordRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('update')]
    public function update(TalkKeywordRequest $request): \Psr\Http\Message\ResponseInterface
    {

        $data = $request->all();
        $groupData['group_id'] = $data['group_id'];
        $groupData['title'] = $data['title'];
        $groupData['keyword'] = $data['keyword'];
        $groupData['match_type'] = $data['match_type'];
        $groupData['content'] = $data['content'];
        return $this->wordService->update($request->input('id'),$groupData) ? $this->success() : $this->error();
    }

    /**
     * 删除
     * @param TalkKeywordRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('delete')]
    public function delete(TalkKeywordRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->wordService->delete([$request->input('id')]) ? $this->success() : $this->error();
    }

    /**
     * 搜索关键字获取匹配的内容
     * @param TalkKeywordRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('searchKeywords')]
    public function searchKeywords(TalkKeywordRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data =$request->all();
        $data['main_uid'] = \Hyperf\Context\Context::get('main_uid');
        return $this->success($this->service->searchKeywords($data));
    }
}